<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redis;
use App\Http\Controllers\Controller;
use App\Model\Goods;
use App\Model\Price;
use App\Model\Settings;
use DB;
use App\Http\Controllers\Common\Search;
use App\Http\Controllers\Common\CommonController;


class IndexController extends Controller
{

	//首页
	public function Index()
	{



		$aa = Redis::get('type');
		$aa = json_decode($aa);
		if ( !$aa ) {

			// 制作三级导航栏数据
			$type = DB::table('shop_type')->select('id','name','pid')->where('pid' , 0)->get();
			foreach ($type as  $v) {
				 $aa[$v->name] = [];
				 $child= DB::table('shop_type')->select('id','name','pid')->where('pid' , $v->id)->get();
				foreach ($child as $cc) {
					$aa[$v->name][$cc->name] = [];
					$grandson = DB::table('shop_type')->select('id','name','pid')->where('pid' , $cc->id)->get();
					foreach ($grandson as $kk ) {
						 $aa[$v->name][$cc->name][$kk->id] = $kk->name;
					}
				}
			}
			Redis::set('type', json_encode($aa));
			Redis::expire('type', 36000);
		}

		// 热销商品
		$hot_goods = Redis::get('hot_goods');
		$hot_goods = json_decode($hot_goods);
		if (!$hot_goods) {
			$hot_goods = Goods::select(['id','gname','goods_sales','clicknum','pic','goods_store'])
				->where([
					[ 'status', '<>', '3'],
					[ 'clicknum' , '>=' , '150' ]
				])->orderBy('goods_sales', 'desc')->limit(9)->get();

			Redis::set('hot_goods', json_encode($hot_goods));
			Redis::expire('hot_goods', 6000);
			foreach($hot_goods as &$val) {
				$price = DB::table('shop_price')->select(min(['price']))->where('gid',$val->id)->first();
				$val->price = $price->price;
				Redis::set( 'good'.$val->id, json_encode($val));
				Redis::expire( 'good'.$val->id, 36000);
			}
			Redis::set( 'hot_goods', json_encode($hot_goods));
			Redis::expire( 'hot_good', 36000);
		}
// 		// 在首页中遍历商品表并加入xunsearh
		$search = new Search('lamp29');
		// dd($search);
		$search->cleanIndex($data);
		$searchGoods= DB::table('shop_goods')->join('shop_price','shop_goods.id','=','shop_price.gid')->select('shop_goods.id','shop_goods.gname','shop_goods.goods_sales','shop_goods.pic','shop_price.price')->get();
		// dd($searchGoods);
		
		foreach ($searchGoods as $goods) {
		$data = [
			'id'=>$goods->id,
			'gname'=>$goods->gname,
			'pic'=>$goods->pic,
			'price'=>$goods->price,
		];

	       	 $search->addDocumentData($data);
		}
	         return view('Home/Index' , [ 
	                    'hot_goods' => $hot_goods,
	                    'type' => $aa,
	                ]);
	}
    //搜索页面
    public function searchInList(Request $request)
    {
        // 接收表单提交过来的东西
        $keyword = $request->input('keyword');
        // dd($keyword);
        if (empty($keyword)) {
            return back();
        }
        $search = new Search('lamp29');
        $searchRes = $search->dosearch("gname:".$keyword);

        // 对搜索到的商品进行分页
        $pageRes = CommonController::CustomPagination($request, $searchRes, 4);
        // dd($searchRes);
        //查出id 商品名 价格和销量
        // $searchBase = DB::table('shop_goods')->join('shop_price','shop_goods.id','=','shop_price.gid')->select('shop_goods.id','shop_goods.gname','shop_goods.sales','shop_goods.pic','shop_price.price')->where()->get();
        // dd($searchBase);
        // dd($searchRes);
        return view('Home/searchList',['pageRes'=>$pageRes,'keyword'=>$keyword,'listData'=>$searchRes]);
    }


	// 详情页
	public function Detail($gid)
	{
		// 搜索商品详情
		$goods = Redis::get('good'.$gid);
		$goods = json_decode($goods);
		if (!$goods) {
			$goods =  DB::table('shop_goods')->where('id',$gid)->first();
			if (!$goods) {
					return redirect('/');
			}
			$goodsprice = DB::table('shop_price')->select(min(['price']))->where('gid',$gid)->first();
			$goods->price =  $goodsprice->price;
		}
		$price = DB::table('shop_price')->select('price','id','color')->where('gid', $gid);
		$max = $price->max('price');
		$min = $price->min('price');
		$color = DB::table('shop_price')->select('color')->where('gid', $gid)->groupBy('color')->get();
		$memory = DB::table('shop_price')->select('memory')->where('gid', $gid)->groupBy('memory')->get();
		 //图片
		$pri = DB::table('shop_price')->select('price','id','color')->where('gid', $gid)->get();
		  foreach($pri as $v){
			  $pc[] =  DB::table('shop_pic')->select('pic')->where('gid',$v->id)->first();
		  }

		// 产品参数
		   $text = DB::table('shop_text')->select('attribute')->where('gid',$gid)->first();
			if ($text) {
			   $doc = explode( '/' , $text->attribute);
			} else {
				$doc = [];
			}
		// 推荐商品
		$dgoods = DB::table('shop_goods')
				->join('shop_price', 'shop_price.gid', '=', 'shop_goods.id')
				->select('shop_goods.id','shop_goods.gname','shop_goods.goods_sales','shop_goods.clicknum','shop_goods.pic',min(['shop_price.price']) ,'shop_price.memory','shop_price.color')
				->orderBy('clicknum', 'desc')
				->limit(12)->get();


		return view( 'Home/Detail',[
					'DetailData' => $goods ,
					'color' => $color,
					'memory' => $memory,
					'max' => $max ,
					'min' => $min,
					'goods' => $dgoods,
					'doc' => $doc,
					'pic' => $pc,
					]
		);
	}

	// 注册
	public function Register()
	{
		$set = Settings::select('registerCode')->first();
		$registerCode = $set->registerCode;
		return view('Home/Register', ['registerCode' => $registerCode]);
	}
	// 注销登录
	public function Logout()
	{
		return view('Home/Index');
	}
	// 登录
	public function Login()
	{
		return view('Home/Login');
	}



	// public function Success()
	// {
	// 	$order = DB::table('orders')
	// 			->orderBy('id', 'desc')
	// 			->first();
	// }
	// 商品列表
	public function List()
	{
		//将两张表拼接起来
		$listData = DB::table('shop_goods')
            ->join('shop_price', function($join)
            {
                $join->on('shop_goods.id', '=', 'shop_price.gid');
            })->select('shop_goods.id','shop_goods.gname','shop_name.pic','shop_price.price','shop_goods.goods_sales')
              ->orderBy('shop_goods.id', 'desc')
              ->get();
		return view('Home/List');
	}

    //意见页面模板
    public function Suggest()
    {
        return view('Home/Suggest');
    }

    public function Success()
    {
         $order = DB::table('orders')
                ->orderBy('id', 'desc')
                ->first();
                $id = $order->id;
        $address = $order->adderssname;
        $man = $order->linkman;
        $phone = $order->telephone;
        $money = $order->total_money;
        return view('Home/Success',['order'=>$order,'money'=>$money,'address'=>$address,'man'=>$man,'phone'=>$phone,'id'=>$id]);
    
	}
}
